package com.lottery.material.controller.admin.conf;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.pojo.conf.UserLevelDict;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojocustom.conf.UserLevelDictCustom;
import com.lottery.material.service.admin.conf.UserLevelDictService;
import com.lottery.material.util.Jurisdiction;
import com.lottery.material.util.PdUtil;
import com.lottery.material.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：用户等级字典Controller
 * Created by 朱正磊 on 2019-05-17 10:41
 */
@Controller("adminUserLevelDictController")
@RequestMapping("/admin/userLevelDict")
public class UserLevelDictController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/userLevelDict/list";

    private final UserLevelDictService userLevelDictService;

    @Autowired
    public UserLevelDictController(UserLevelDictService userLevelDictService) {
        this.userLevelDictService = userLevelDictService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        page.setPd(pd);

        //查询分页列表
        List<UserLevelDictCustom> list = userLevelDictService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        return new ModelAndView("conf/userLevelDict/listPage", map);
    }

    /**
     * description：添加
     * user 朱正磊
     * time 2019-04-17 16:39
     * @return 返回响应对象
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add() {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {//判断是否有添加权限
            return null;
        }
        Integer flag = userLevelDictService.add();
        if (Objects.equals(0, flag)) {
            return ResponseResult.buildResult(400, "添加失败！");
        } else {
            return ResponseResult.buildResult(200, "添加成功！");
        }
    }

}
